package com.cuckoo.mall.thus.menuindex;


import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bgn.baseframe.base.MVPBaseFragment;
import com.bgn.baseframe.utils.ToastUtil;
import com.bgn.baseframe.utils.UiUtil;


import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.Category;
import com.cuckoo.mall.javabean.ImageItem;
import com.cuckoo.mall.thus.menuindex.adapter.MyViewPageAdapter;
import com.cuckoo.mall.thus.menuindex.adapter.TopBannerAdapter;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MenuIndexFragment extends MVPBaseFragment<MenuIndexContract.View, MenuIndexPresenter> implements MenuIndexContract.View {


    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;

    @BindView(R.id.rv_lunbo)
    RollPagerView rvLunbo;

    @BindView(R.id.vp_category)
    ViewPager vpCategory;
    @BindView(R.id.main_content)
    CoordinatorLayout mainContent;

    /*缺省页*/
    @BindView(R.id.lly_defalut)
    LinearLayout llyDefalut;

    /*适配器*/
    private TopBannerAdapter topBannerAdapter;
    private MyViewPageAdapter myViewPageAdapter;
    /*数据源*/
    private List<Category> categories = new ArrayList<>();

    @Override
    protected View initView(LayoutInflater inflater) {
        View rootView = inflater.inflate(R.layout.fragment_menu_index, null);
        ButterKnife.bind(this, rootView);
        initIndexView();
        return rootView;
    }

    private void initIndexView() {
        // initTopBanner();
        initIndicator();
    }

    private void initTopBanner() {
        rvLunbo.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW, Color.WHITE));
        rvLunbo.getLayoutParams().height = UiUtil.getScreenWidth() * 1 / 1;
        topBannerAdapter = new TopBannerAdapter(getLunboData(), getActivity(), rvLunbo);
        rvLunbo.setAdapter(topBannerAdapter);
        rvLunbo.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ToastUtil.showInfo(position + "");
            }
        });
        topBannerAdapter.notifyDataSetChanged();
    }

    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return categories == null ? 0 : categories.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);

                simplePagerTitleView.setText(categories.get(index).getCategory_name());
                simplePagerTitleView.setNormalColor(UiUtil.getColor(R.color.black_999999));
                simplePagerTitleView.setSelectedColor(UiUtil.getColor(R.color.red));
                simplePagerTitleView.setTextSize(15);

                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vpCategory.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
//                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
//                indicator.setStartInterpolator(new AccelerateInterpolator());
//                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(UiUtil.getColor(R.color.red));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, vpCategory);
    }

    private void initViewPage() {
        vpCategory.setOffscreenPageLimit(1);
        myViewPageAdapter = new MyViewPageAdapter(getChildFragmentManager(), categories);
        vpCategory.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vpCategory.setCurrentItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vpCategory.setAdapter(myViewPageAdapter);
        myViewPageAdapter.notifyDataSetChanged();
    }


//    private void initIndicator() {
//
//        CommonNavigator commonNavigator7 = new CommonNavigator(getActivity());
//        commonNavigator7.setScrollPivotX(0.65f);
//        commonNavigator7.setAdapter(new CommonNavigatorAdapter() {
//            @Override
//            public int getCount() {
//                return categories == null ? 0 : categories.size();
//            }
//
//            @Override
//            public IPagerTitleView getTitleView(Context context, final int index) {
//                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
//                simplePagerTitleView.setText(categories.get(index).getCategory_name());
//                simplePagerTitleView.setText(categories.get(index).getCategory_name());
//                simplePagerTitleView.setNormalColor(UiUtil.getColor(R.color.black_414141));
//                simplePagerTitleView.setSelectedColor(UiUtil.getColor(R.color.black));
//                simplePagerTitleView.setTextSize(16);
//                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        vpCategory.setCurrentItem(index);
//                    }
//                });
//                return simplePagerTitleView;
//            }
//
//            @Override
//            public IPagerIndicator getIndicator(Context context) {
//                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
//
////                LinePagerIndicator indicator = new LinePagerIndicator(context);
////                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
////                indicator.setStartInterpolator(new AccelerateInterpolator());
////                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
//                indicator.setColors(UiUtil.getColor(R.color.red));
//                return indicator;
//            }
//        });
//        magicIndicator.setNavigator(commonNavigator7);
//        ViewPagerHelper.bind(magicIndicator, vpCategory);
//    }

//    private void initIndicator() {
//
//        magicIndicator.setBackgroundColor(Color.WHITE);
//        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
//        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
//            @Override
//            public int getCount() {
//                return categories == null ? 0 : categories.size();
//            }
//
//            @Override
//            public IPagerTitleView getTitleView(Context context, final int index) {
//                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
//                simplePagerTitleView.setText(categories.get(index).getCategory_name());
//                simplePagerTitleView.setTextSize(18);
//                simplePagerTitleView.setNormalColor(Color.GRAY);
//                simplePagerTitleView.setSelectedColor(Color.BLACK);
//                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        vpCategory.setCurrentItem(index);
//                    }
//                });
//                return simplePagerTitleView;
//            }
//
//            @Override
//            public IPagerIndicator getIndicator(Context context) {
//                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
//                indicator.setColors(Color.parseColor("#ff4a42"), Color.parseColor("#fcde64"), Color.parseColor("#73e8f4"), Color.parseColor("#76b0ff"), Color.parseColor("#c683fe"));
//                return indicator;
//            }
//        });
//        magicIndicator.setNavigator(commonNavigator);
//        ViewPagerHelper.bind(magicIndicator, vpCategory);
//    }

    @Override
    protected void initData() {
        if (isNetworkerConnectHint()) {
            showLoadingView();
            mPresenter.loadAllCategorys();
        } else {
            //showDefaultView();
            llyDefalut.setVisibility(View.VISIBLE);
        }
        llyDefalut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        TextView textView = llyDefalut.findViewById(R.id.tv_load);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }


    private List<ImageItem> getLunboData() {
        List<ImageItem> items = new ArrayList<>();
        ImageItem imageItem = new ImageItem();
        imageItem.setImageUrl("https://images2018.cnblogs.com/news/24442/201806/24442-20180611171134854-1629599186.jpg");
        items.add(imageItem);
        items.add(imageItem);
        items.add(imageItem);
        return items;
    }

    @Override
    public void whenGetCategorysSucceed(List<Category> datas) {
        llyDefalut.setVisibility(View.GONE);
        categories.addAll(datas);
        initViewPage();
        magicIndicator.getNavigator().notifyDataSetChanged();

    }

    @Override
    public void whenGetCategorysFaild(String message) {
        llyDefalut.setVisibility(View.VISIBLE);
        TextView textView = llyDefalut.findViewById(R.id.tv_hint);
        textView.setText(message);
    }
}
